package com.crr.controller;

import com.crr.mapper.AirQualityIndexMapper;
import com.crr.mapper.DistrictMapper;
import com.crr.pojo.AirQualityIndex;
import com.crr.pojo.District;
import com.crr.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AirQualityIndexController {
    @Autowired
    private AirQualityIndexMapper airQualityIndexMapper;
    @Autowired
    private DistrictMapper districtMapper;

    @RequestMapping("show")
    public ModelAndView getInfoByPage(int pageSize, int pageNum) {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("pageNum", pageSize * (pageNum - 1));
        map1.put("pageSize", pageSize);
        List<AirQualityIndex> list = airQualityIndexMapper.getInfoByPage(map1);
        int count = airQualityIndexMapper.getInfoNum();//信息总数
        int totalPages = PageUtil.getTotalPages(count, pageSize);//总页数
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("info", list);
        mv.addObject("totalPages", totalPages);
        mv.addObject("count", count);
        mv.addObject("pageSize", pageSize);
        mv.addObject("pageNum", pageNum);
        return mv;
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object>  addInfo(@RequestBody AirQualityIndex airQualityIndex) {
        System.out.println(airQualityIndex);
      /*  System.out.println(name);*/
        Map<String, Object> map=new HashMap<>();
   /*     District d = districtMapper.getDistrictIdByName(airQualityIndex.getDistrictId());
        airQualityIndex.setDistrictId(d.getId());*/
        int result = airQualityIndexMapper.addInfo(airQualityIndex);
        if (result == 1) {
            map.put("status", "success");
            map.put("message", "信息添加成功！！！");
        }else{
            map.put("status", "error");
            map.put("message", "信息添加失败！");
        }
        return map;
    }


}
